package com.thread.consumeandproduct;

import java.util.concurrent.Callable;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

public class ConsumerJetCom implements Callable<ProductJetBean> {


    private LinkedBlockingQueue<ProductJetBean> resultBlockQueue = new LinkedBlockingQueue<>(1);
    private ProductJetBean productBean;


    public void setProductBean(ProductJetBean productBean) {
        this.productBean = productBean;
    }

    public void receive(String content) {
        if (productBean.getContent().equals(content)) {
            resultBlockQueue.offer(productBean);
        }
    }

    @Override
    public ProductJetBean call() {
        ProductJetBean pb;
        try {
            System.out.println("call");
            if (!productBean.isValid()) {
                return productBean;
            }
            pb = resultBlockQueue.poll(productBean.getTimeOut(), TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
            return null;
        }
        return pb;
    }
}
